package io.renren.controller;

import io.renren.annotation.Login;
import io.renren.common.exception.RRException;
import io.renren.common.utils.ConfigConstant;
import io.renren.common.utils.DateUtils;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.ProjectConstant;
import io.renren.common.utils.R;
import io.renren.common.validator.Assert;
import io.renren.common.validator.ValidatorUtils;
import io.renren.form.CallbackForm;
import io.renren.service.BalanceTopupService;
import io.renren.service.SysConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.Date;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import springfox.documentation.annotations.ApiIgnore;

import com.alibaba.fastjson.JSON;

/**
 * 会员接口
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2017-03-23 15:31
 */
@RestController
@RequestMapping("/api/topup")
@Api(tags = "充值提现接口")
public class ApiTopupController extends AbstractController {

	@Autowired
	private BalanceTopupService balanceTopupService;

	@Autowired
	private SysConfigService sysConfigService;

	@Login
	@PostMapping("listDeposit")
	@ApiOperation("充值列表")
	public R listDeposit(@ApiIgnore @RequestAttribute("userId") Long userId, @ApiIgnore @RequestParam Map<String, Object> params) {
		params.put("type", ProjectConstant.ORDER_TYPE_DEPOSIT);
		params.put("memberId", userId);
		PageUtils page = balanceTopupService.queryPage(params);
		return R.ok().put("page", page);
	}

	@Login
	@PostMapping("listWithdraw")
	@ApiOperation("提现列表")
	public R listWithdraw(@ApiIgnore @RequestAttribute("userId") Long userId, @ApiIgnore @RequestParam Map<String, Object> params) {
		params.put("type", ProjectConstant.ORDER_TYPE_WITHDRAW);
		params.put("memberId", userId);
		PageUtils page = balanceTopupService.queryPage(params);
		return R.ok().put("page", page);
	}

	@Login
	@PostMapping("submitDeposit")
	@ApiOperation("充值提交")
	public R submitDeposit(@ApiIgnore @RequestAttribute("userId") Long userId, @RequestParam Double amount, @RequestParam Integer paymentType) {
		Assert.isTrue(amount != null && amount > 0 && paymentType != null, "参数有误");
		balanceTopupService.submitDeposit(userId, amount, paymentType);
		return R.ok().put("serviceUrl", sysConfigService.getValue(ConfigConstant.THIRD_PAYMENT_URL));
	}

	@PostMapping("callbackDeposit")
	@ApiOperation("充值回调")
	public R callbackDeposit(@ApiIgnore @RequestAttribute("userId") Long userId, @RequestParam String json) {
		Assert.hasText(json, "参数有误");
		System.out.println("json");
		CallbackForm form = JSON.parseObject(json, CallbackForm.class);
		// 校验类型
		ValidatorUtils.validateEntity(form);
		balanceTopupService.callbackDeposit(form);
		return R.ok();
	}

	@Login
	@PostMapping("submitWithdraw")
	@ApiOperation("提现提交")
	public R submitWithdraw(@ApiIgnore @RequestAttribute("userId") Long userId, @RequestParam Double amount, @RequestParam Integer paymentType) {
		Assert.isTrue(amount != null && amount > 0 && paymentType != null, "参数有误");

		Date now = new Date();
		String startOfDay = DateUtils.format(now);
		// 判断提现时间
		String[] withdrawTime = sysConfigService.getValue(ConfigConstant.WITHDRAW_TIME).split("-");
		Date openTime = DateUtils.stringToDate(startOfDay + " " + withdrawTime[0], DateUtils.DATE_TIME_PATTERN);
		Date closeTime = DateUtils.stringToDate(startOfDay + " " + withdrawTime[1], DateUtils.DATE_TIME_PATTERN);
		int compare = closeTime.compareTo(openTime);
		if (compare == 1) {
			// 未跨天
			if (openTime.after(now) || closeTime.before(now)) {
				throw new RRException("每日提现时间为" + withdrawTime[0].substring(0, 5) + "-" + withdrawTime[1].substring(0, 5));
			}
		} else if (compare == -1) {
			// 跨天
			if (now.before(openTime) && now.before(closeTime)) {
			} else if (now.after(openTime) && now.after(closeTime)) {
			} else {
				throw new RRException("每日提现时间为" + withdrawTime[0].substring(0, 5) + "-" + withdrawTime[1].substring(0, 5));
			}
		}

		balanceTopupService.submitWithdraw(userId, amount, paymentType);
		return R.ok();
	}

	@PostMapping("callbackWithdraw")
	@ApiOperation("提现回调")
	public R callbackWithdraw(@ApiIgnore @RequestAttribute("userId") Long userId, @RequestParam String json) {
		Assert.hasText(json, "参数有误");
		System.out.println("json");
		CallbackForm form = JSON.parseObject(json, CallbackForm.class);
		// 校验类型
		ValidatorUtils.validateEntity(form);
		balanceTopupService.callbackWithdraw(form);
		return R.ok();
	}
}
